package barbershop.com.controller.statistic;

import barbershop.com.entity.ServiceType;
import barbershop.com.entity.Services;
import barbershop.com.service.ServiceTypeService;
import barbershop.com.service.ServicesService;
import barbershop.com.service.statistic.StatisticService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/stat")
public class StatisticController {
    @Autowired
    private StatisticService statisticService;
    @Autowired
    private ServiceTypeService serviceTypeService;
    @Autowired
    private ServicesService servicesService;

    /*@RequestMapping("/service")
    public Object statService(){
        JSONObject object = new JSONObject();
        List<ServiceType> serviceTypeList = this.serviceTypeService.findAll();
        for (ServiceType serviceType: serviceTypeList){
            Integer sum = this.statisticService.getServiceStat(serviceType.getServiceTypeId());
            object.put(serviceType.getServiceTypeName(), sum);
        }
        return object;
    }*/


    @RequestMapping("/dailyTotal")
    public Object getDailyTotal() {
        JSONObject object = new JSONObject();
        ArrayList<String> dateList = this.statisticService.getCurrentWeekDays();
        for (String date : dateList) {
            Double totalAmount = this.statisticService.getDailyTotalStat(date);
            object.put(date, totalAmount);
        }
        return object;
    }

    @RequestMapping("/weeklyServiceType")
    public Object statServiceType() {
        JSONObject object = new JSONObject();
        List<ServiceType> serviceTypeList = this.serviceTypeService.findAll();
        for (ServiceType serviceType : serviceTypeList) {
            Integer sum = this.statisticService.getServiceTypeWeeklyStat(serviceType.getServiceTypeId());
            object.put(serviceType.getServiceTypeName(), sum);
        }
        return object;
    }

    @RequestMapping("/weeklyService")
    public List<JSONObject> statService() {
        List<JSONObject> objectList = new ArrayList<>();
        List<Services> serviceList = this.servicesService.findAll();
        for (Services service : serviceList) {
            JSONObject object = new JSONObject();
            Integer sum = this.statisticService.getServiceWeeklyStat(service.getServiceId());
            object.put("name", service.getServiceGrade());
            object.put("value", sum);
            System.out.println(object);
            objectList.add(object);
        }
        return objectList;
    }

    @RequestMapping("/complex")
    public List<JSONObject> complexStat(@RequestParam(required = false)String startDate,
                                        @RequestParam(required = false)String endDate){
        List<JSONObject> objectList = new ArrayList<>();
        List<ServiceType> serviceTypeList = this.serviceTypeService.findAll();
        // 统计服务类型
        JSONObject servTypeObject = new JSONObject();
        for (ServiceType serviceType : serviceTypeList) {
            Integer sum =
                    this.statisticService.getServiceTypeStat(serviceType.getServiceTypeId(),startDate, endDate);
            servTypeObject.put(serviceType.getServiceTypeName(), sum);
        }
        // 统计营业额
        JSONObject dailyTotalObject = new JSONObject();
        ArrayList<String> dateList = this.statisticService.getIntervalDays(startDate, endDate);
        for (String date : dateList) {
            Double totalAmount = this.statisticService.getDailyTotalStat(date);
            dailyTotalObject.put(date, totalAmount);
        }
        // 汇总
        objectList.add(servTypeObject);
        objectList.add(dailyTotalObject);
        return objectList;
    }
}
